Ext.define('AM.view.AdviceView', {
		extend: 'Ext.grid.Panel',
		alias : 'widget.adviceView',
		store : 'AdviceStore',
		id:'adviceView',
		height: 200,
		width: 400,
		frame:true,
		columnLines:true,
//		selModel : Ext.create('Ext.selection.CheckboxModel'), 
		selModel: {
	        injectCheckbox: 0,
	        mode: "SIMPLE"  //"SINGLE"/"SIMPLE"/"MULTI"
//	        checkOnly: true     //只能通过checkbox选择
   		 },
   		selType: "checkboxmodel",
    	dockedItems: [{
        xtype: 'pagingtoolbar',
        dock: 'bottom',
        store : 'AdviceStore',
        displayInfo: true
    }],
		columns: [	
			        {text: '序号', xtype: 'rownumberer',width:50,align:'center'},
			        { text: 'ID', dataIndex: 'id', width:0,align:'center'},
			        { text: '内容', dataIndex: 'adcontent', width:80,align:'center'}
			     ],
			   tbar: [
					  { xtype: 'button', text: '增加',handler: addAdvice,icon: 'Ext/restful/images/add.png'
					  },
					  { xtype: 'button', text: '修改',handler: editAdvice,icon: 'Ext/restful/images/cog_edit.png'
					  },
					  { xtype: 'button', text: '删除',handler: deleteAdvice,icon: 'Ext/restful/images/delete.png'
					  }
					]
		
})

 function addAdvice() {
		 var myform1= new Ext.form.Panel({
			        frame:true,
			        width : 700, 
			        height : 200,
			        bodyPadding: 5,
			        frame:true,
			        labelWidth : 65,
					   items: [{   
					   		      columnWidth :.46,
					   			  xtype : "textarea",
					   		      fieldLabel: '建议',
					   			  labelAlign :"right",
							      name: 'adcontent',
								  height : 100,
							      width:500
							}]		        	
				        	       

		 })
			var win = new Ext.Window({
				title : "添加建议",
				width : 700,
				height : 200,
				frame:true,
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'advice_addAdvice',
											 success : function() {
												Ext.Msg.alert('提示', '添加成功');
												win.close();
												Ext.getCmp('adviceView').getStore().reload();//jobView 为id号
											},
											failure : function() {
												Ext.Msg.alert('错误', '添加失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
								win.close();
											
							}
						}]
			});
		win.show();
	}



   function editAdvice() {
		 var myform1= new Ext.form.Panel({
			        frame:true,
			        width : 700,
			        height : 200, 
			        frame:true,
			        labelWidth : 65,
			        items: [{   
					   			  xtype : "textarea",
					   		      fieldLabel: '建议',
					   			  labelAlign :"right",
							      name: 'adcontent',
							      height : 100,
							      width:500
							},{
								  xtype : "textfield",
					   		      fieldLabel: '建议id',
					   			  labelAlign :"right",
							      name: 'id',
							      hidden : true 
							}]		        	
		
		 })
			var win = new Ext.Window({
				title : "修改建议",
				width : 700,
				height : 200,
				frame:true,
				items : [ myform1
							],
				buttons : [{
							text : "确定",
							handler : function() {
								win.getComponent(0).getForm().submit({
											 url : 'advice_editAdvice',
											 success : function() {
												Ext.Msg.alert('提示', '修改成功');
												win.close();
												Ext.getCmp('adviceView').getStore().reload();
												Ext.getCmp('adviceView').getSelectionModel().deselectAll();
											},
											failure : function() {
												Ext.Msg.alert('错误', '修改失败');
											},
											waitMsg : "请稍后，正在保存。。。。"
	
										});
							}
						}, {
							text : "重置",
							handler : function() {
								win.getComponent(0).getForm().reset();
							}
						}, {
							text : "取消",
							handler : function() {
								win.close();
											
							}
						}]
			});
		var grid=this.ownerCt.ownerCt;
		var selections = grid.getSelectionModel().getSelection();
						if(selections.length==0){
							Ext.MessageBox.alert("提示","请选择要编辑的一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							var record = selections[0];
						//这里是修改操作的实现部分
							win.getComponent(0).getForm().loadRecord(record);
							win.show();
						}
	}

	function deleteAdvice(){
				 var grid=this.ownerCt.ownerCt;
			     var selections = grid.getSelectionModel().getSelection();
					if(selections){
					if(selections.length==0){
							Ext.MessageBox.alert("提示","请检查是否选中某一行！");
						}else if (selections.length > 1) {
							Ext.MessageBox.alert("提示", "请检查您是否选中了多行！");
							return;
						} else{
							Ext.MessageBox.confirm("提示", "确认要删除 ？", function(btn) {
							if (btn == 'yes') {			
							var record = selections[0];
								Ext.Ajax.request({
										url : 'advice_deleteAdvice',
										success : function() {
											Ext.MessageBox.alert("提示",	"删除成功！");
													 grid.getStore().remove(selections );
												     grid.getStore().reload();
										},
										failure : function() {
											Ext.MessageBox.alert("提示", "删除失败！");
										},
										params : {
											id : record.get("id")
										}
									});			
								}
					  	})
                   	} 
 			   
		   }				
	}
	 	

	